﻿Imports System.IO

Public Class Animales

    Dim nombre, especie, observaciones As String
    Dim fecha As Date
    Dim refCli, id As Integer


    Private Sub Animales_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Me.CenterToScreen()
    End Sub

    Private Sub Animales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.ClientesTableAdapter.Fill(Me.NuevoZooDBDataSet.Clientes)

            Me.CitasTableAdapter.Fill(Me.NuevoZooDBDataSet.Citas)

            Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)


            ConCombo.SelectedIndex = -1
        Catch ex As Exception
            MsgBox("Error al conectar con la base de datos", MsgBoxStyle.Critical, "Error")
        End Try


        ConCombo.SelectedIndex = -1

        If (Login.encargadologin = True) Then
            BElimAnim.Enabled = True
        Else
            BElimAnim.Enabled = False

        End If
    End Sub

    Private Sub TxtNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNombre.TextChanged
        Me.AnimalesTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Animales, TxtNombre.Text)

    End Sub





    Private Sub TxtElNom_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtElNom.TextChanged
        Me.AnimalesTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Animales, TxtElNom.Text)
    End Sub

    Private Sub TxtEdNom_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtEdNom.TextChanged
        Me.AnimalesTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Animales, TxtEdNom.Text)
    End Sub



    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim dr As New DataGridViewRow
        dr = DataGridView1.SelectedRows(0)
        TextBox1.Text = dr.Cells(0).Value.ToString
        TextBox2.Text = dr.Cells(1).Value.ToString
        TextBox5.Text = dr.Cells(2).Value.ToString
        TextBox3.Text = dr.Cells(3).Value.ToString.Substring(0, 10)
        TextBox4.Text = dr.Cells(4).Value.ToString
        TextBox6.Text = dr.Cells(5).Value.ToString
    End Sub

    Private Sub DataGridView3_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellContentClick
        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        TextBox8.Text = dr.Cells(0).Value.ToString
        EdNom.Text = dr.Cells(1).Value.ToString
        TextBox7.Text = dr.Cells(2).Value.ToString
        TextBox9.Text = dr.Cells(3).Value.ToString.Substring(0, 10)
        EdCli.Text = dr.Cells(4).Value.ToString
        EdObs.Text = dr.Cells(5).Value.ToString
    End Sub

    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)
        TextBox15.Text = dr.Cells(0).Value.ToString
        TextBox14.Text = dr.Cells(1).Value.ToString
        TextBox11.Text = dr.Cells(2).Value.ToString
        TextBox13.Text = dr.Cells(3).Value.ToString.Substring(0, 10)
        TextBox12.Text = dr.Cells(4).Value.ToString
        TextBox10.Text = dr.Cells(5).Value.ToString
    End Sub


    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView2.SelectedRows(0)
        TxtCli.Text = dr.Cells(1).Value.ToString
        refCli = dr.Cells(0).Value
    End Sub



    Private Sub BAddAnim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BAddAnim.Click
        nombre = TxtNom.Text
        especie = ComboEsp.Text
        fecha = DateTimePicker1.Text

        observaciones = TxtObs.Text

        Dim query As IEnumerable(Of Decimal) = _
                From item As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                Where item.Cells(0).Value IsNot DBNull.Value _
                Select CDec(item.Cells(0).Value)

        ' Obtenemos el valor máximo
        '
        Dim id As Integer = query.ToList.Max() + 1
        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para dar de alta a un animal", , "Error")
        ElseIf especie = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para dar de alta a un animal", , "Error")
        ElseIf refCli = 0 Or Login.validar.ValidarInt(refCli, 1, 600) <> 3 Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para dar de alta a un animal", , "Error")
        ElseIf id = 0 Then
            MsgBox("Debe seleccionar un registro de la tabla", , "Error")
        Else

            Try
                Me.AnimalesTableAdapter.Insert(id, nombre, especie, fecha, refCli, observaciones)
                Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)
                MsgBox("Registro añadido correctamente")
                Using writer As New StreamWriter("D:\log.txt", True)
                    writer.WriteLine("El usuario " & Login.usuariologin.ToUpper & " ha añadido un registro de Animal de ID " & id & ".   " & DateTime.Now)
                End Using
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If

    End Sub

    Private Sub BElimAnim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BElimAnim.Click

        If nombre <> "" Then
            Dim respuesta As MsgBoxResult = MsgBox("¿Desea eliminar el registro?", MsgBoxStyle.YesNo, "Confirmación")
            If respuesta = MsgBoxResult.Yes Then
                Try
                    Me.AnimalesTableAdapter.Delete(id)
                    Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)
                Catch ex As Exception
                    MsgBox("No se pudo borrar el registro", MsgBoxStyle.Critical, "Error")
                End Try

            Else

            End If
        Else
            MsgBox("Seleccione un registro para borrar.", MsgBoxStyle.OkOnly, "Datos incorrectos")
        End If
    End Sub


    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        id = dr.Cells(0).Value
        EdNom.Text = dr.Cells(1).Value.ToString
        EdCli.Text = dr.Cells(4).Value.ToString
        EdObs.Text = dr.Cells(5).Value.ToString


    End Sub

    Private Sub BEdAnim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BEdAnim.Click

        nombre = EdNom.Text
        especie = TextBox7.Text
        fecha = TextBox9.Text
        Dim number As Integer
        If (Int32.TryParse(EdCli.Text, number)) = True Then
            refCli = number
        End If

        observaciones = EdObs.Text

        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para modificar un animal", , "Error")
        ElseIf especie = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para modificar un animal", , "Error")
        ElseIf refCli = 0 Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para modificar un animal", , "Error")

        Else


            Try
                Me.AnimalesTableAdapter.UpdateQuery(nombre, especie, fecha, refCli, observaciones, id)
                Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If

    End Sub

    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Me.Close()
        Gestion.Show()

    End Sub
    Private Sub WebsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebsToolStripMenuItem.Click
        Webs.Show()
    End Sub
    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        TabControl1.SelectedIndex = 3
    End Sub
    Private Sub EditarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        TabControl1.SelectedIndex = 2
    End Sub


    Private Sub NuevoToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
        TabControl1.SelectedIndex = 1

    End Sub

    Private Sub AyudaToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AyudaToolStripMenuItem1.Click
        Dim rutaEjecutable As String = System.IO.Directory.GetCurrentDirectory().ToString
        System.Diagnostics.Process.Start(rutaEjecutable & "\Ayuda.chm")
    End Sub
End Class
